package com.yk.faceapplication.activity;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.MyApplication;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/12_16:29
 */

public class UpdatePwdActivity extends BaseActivity{
    private EditText pwdET,newPwdET,rePwdET;
    private Button commitBtn;
    private Location location;

    private ImageView pwdEyeIV,newPwdEyeIV,newPwdAgainEyeIV;
    private boolean pwdIsOpen = false;
    private boolean newPwdIsOpen = false;
    private boolean newPwdAgainIsOpen = false;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("修改登录密码");
        pwdEyeIV = (ImageView) findViewById(R.id.pwd_eye_img);
        pwdEyeIV.setOnClickListener(this);
        newPwdEyeIV = (ImageView) findViewById(R.id.new_pwd_eye_img);
        newPwdEyeIV.setOnClickListener(this);
        newPwdAgainEyeIV = (ImageView) findViewById(R.id.new_pwd_again_eye_img);
        newPwdAgainEyeIV.setOnClickListener(this);

        pwdET = (EditText) findViewById(R.id.pwd_et);
        newPwdET = (EditText) findViewById(R.id.new_pwd_et);
        rePwdET = (EditText) findViewById(R.id.re_pwd_et);
        commitBtn = (Button) findViewById(R.id.commit_btn);
        commitBtn.setOnClickListener(this);
        location = ((MyApplication) this.getApplicationContext()).getLocation();
        pwdET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        newPwdET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        rePwdET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        checkInputInfo();
    }

    private void checkInputInfo(){
        if(ActivityUtil.editIsNull(newPwdET) || ActivityUtil.editIsNull(pwdET) || ActivityUtil.editIsNull(rePwdET)){
            commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
            commitBtn.setEnabled(false);
        }else{
//            if(newPwdET.getText().toString().trim().equals(rePwdET.getText().toString().trim()) && newPwdET.getText().toString().trim().length()>5
//                    && ActivityUtil.isPassworkFormat(rePwdET.getText().toString().trim())){
//                commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
//                commitBtn.setEnabled(true);
//            }else{
//                commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
//                commitBtn.setEnabled(false);
//            }

            commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
            commitBtn.setEnabled(true);

        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.commit_btn:
                if(ActivityUtil.editIsNull(pwdET) || ActivityUtil.editIsNull(newPwdET)
                        || ActivityUtil.editIsNull(rePwdET)){
                    Toast.makeText(this,"输入不正确",Toast.LENGTH_LONG).show();
                    return;
                }
                if(!newPwdET.getText().toString().trim().equals(rePwdET.getText().toString().trim())){
                    Toast.makeText(this,"两次输入不一致",Toast.LENGTH_LONG).show();
                    return;
                }

                if(!ActivityUtil.isPassworkFormat(rePwdET.getText().toString().trim())){
                    Toast.makeText(this,"密码格式不正确",Toast.LENGTH_LONG).show();
                    return;
                }
                rePwd();
                break;
            case R.id.pwd_eye_img:
//                pwdET,newPwdET,rePwdET;
//                pwdEyeIV,newPwdEyeIV,newPwdAgainEyeIV;
                if(pwdIsOpen){
                    pwdEyeIV.setImageResource(R.drawable.pwd_close_icon);
                    pwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }else{
                    pwdEyeIV.setImageResource(R.drawable.pwd_close_open);
                    pwdET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }
                pwdIsOpen = !pwdIsOpen;
                break;
            case R.id.new_pwd_eye_img:
                //                pwdET,newPwdET,rePwdET;
                //                pwdEyeIV,newPwdEyeIV,newPwdAgainEyeIV;
                if(newPwdIsOpen){
                    newPwdEyeIV.setImageResource(R.drawable.pwd_close_icon);
                    newPwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    newPwdET.setSelection(newPwdET.getText().toString().length());
                }else{
                    newPwdEyeIV.setImageResource(R.drawable.pwd_close_open);
                    newPwdET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    newPwdET.setSelection(newPwdET.getText().toString().length());
                }
                newPwdIsOpen = !newPwdIsOpen;
                break;
            case R.id.new_pwd_again_eye_img:
                //                pwdET,newPwdET,rePwdET;
                //                pwdEyeIV,newPwdEyeIV,newPwdAgainEyeIV;
                if(newPwdAgainIsOpen){
                    newPwdAgainEyeIV.setImageResource(R.drawable.pwd_close_icon);
                    rePwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    rePwdET.setSelection(rePwdET.getText().toString().length());
                }else{
                    newPwdAgainEyeIV.setImageResource(R.drawable.pwd_close_open);
                    rePwdET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    rePwdET.setSelection(rePwdET.getText().toString().length());
                }
                newPwdAgainIsOpen = !newPwdAgainIsOpen;
                break;

        }
    }

    //    修改密码
    private void rePwd(){
        String latitude = location==null?"0":location.getLatitude()+"";
        String longitude = location==null?"0":location.getLongitude()+"";
        Map<String, String> form = new HashMap<String, String>();
        form.put("oldPassword", pwdET.getText().toString().trim());
        form.put("newPassword", newPwdET.getText().toString().trim());
        form.put("latitude", latitude);
        form.put("longitude", longitude);
        form.put("userAgentCode", "2");
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                Toast.makeText(UpdatePwdActivity.this,"密码修改成功",Toast.LENGTH_LONG).show();
                startActivity(new Intent(UpdatePwdActivity.this,LoginActivity.class));
                mApplication.setHomePageGetMember(true);
                mApplication.setToken(null);
                mApplication.setMember(null);
                finishAct();
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.request(this,RequestUrl.UPDATE_PWD+"token="+mToken,callback,form);
    }


}
